/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.payment.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.medusa.gruul.common.core.auth.client.helper.ISecurity;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.handler.Handler;
import com.medusa.gruul.common.core.util.SpringUtils;
import com.medusa.gruul.payment.api.entity.MerchantDetails;
import com.medusa.gruul.payment.api.entity.PaymentMerchantConfig;
import com.medusa.gruul.payment.api.model.dto.MerchantDTO;
import com.medusa.gruul.payment.common.annotation.MerchantHandler;
import com.medusa.gruul.payment.common.helper.WechatHelper;
import com.medusa.gruul.payment.service.IMerchantDetailsService;
import com.medusa.gruul.payment.service.IPaymentMerchantConfigService;
import com.medusa.gruul.payment.service.MultiMerchantService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 张治保
 * date 2021/12/18
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MultiMerchantServiceImpl implements MultiMerchantService {

    private final IMerchantDetailsService merchantDetailsService;
    private final IPaymentMerchantConfigService paymentMerchantConfigService;

    @Override
    public void merchant(MerchantDTO merchant) {
        Handler<Void> handler = SpringUtils.getBean(MerchantHandler.class,merchant.getPayType());
        if (handler == null){
            throw new ServiceException("服务器内部错误,不支持的处理器");
        }
        handler.handle(merchant);
    }


    @Override
    public List<String> getMerchantIds(String tenantId) {
        List<PaymentMerchantConfig> merchantConfigs = paymentMerchantConfigService.lambdaQuery()
            .eq(PaymentMerchantConfig::getTenantId, tenantId)
            .isNull(PaymentMerchantConfig::getPlatform)
            .list();
        if (CollUtil.isEmpty(merchantConfigs)) {
            return Collections.emptyList();
        }
        return merchantConfigs.stream()
            .map(PaymentMerchantConfig::getDetailsId)
            .collect(Collectors.toList());
    }

    @Override
    public List<MerchantDetails> merchants(String tenantId) {
        List<String> merchantIds = this.getMerchantIds(tenantId);
        if (CollUtil.isEmpty(merchantIds)) {
            return Collections.emptyList();
        }
        /*
         *获取店铺配置关系
         */
        return merchantDetailsService.lambdaQuery()
            .in(MerchantDetails::getDetailsId, merchantIds).list();
    }

    @Override
    public String uploadCertificate(MultipartFile multipartFile) {
        if (multipartFile == null || multipartFile.isEmpty()) {
            throw new ServiceException("上传的文件内容为空");
        }
        if (!WechatHelper.nameVerify(multipartFile.getOriginalFilename())) {
            throw new ServiceException("文件格式不正确,请上传微信支付API证书**.p12");
        }

        String absolutePath = WechatHelper.absolutePath(ISecurity.tenantId().must());
        log.info("证书文件路径:{}",absolutePath);
        try {
            multipartFile.transferTo(new File(absolutePath));
        } catch (IOException exception) {
            log.error("保存文件发生错误",exception);
            throw new ServiceException("保存文件时发生错误");
        }
        return absolutePath;
    }

}
